package com.ali.resource.ServicePage;

import com.ali.resource.CompoentPage.RabbitMqComponent;
import com.ali.resource.MapPage.RabbitMq.RabbitMqExchangeMap;
import com.ali.resource.MapPage.RabbitMq.RabbitMqMap;
import com.ali.resource.MapPage.RabbitMq.RabbitMqQueueMap;
import com.ali.resource.MapPage.RabbitMq.RabbitMqVhostMap;
import com.ali.resource.ShareTools.TeaopenapiClient;
import com.aliyun.amqp_open20191212.Client;
import com.aliyun.amqp_open20191212.models.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class RabbitMqService {

    @Autowired
    private RabbitMqComponent rabbitMqComponent;


    @Autowired
    private TeaopenapiClient teaopenapiClient;

    public void getRabbitMqList() throws Exception {

        // 接收数组
        ArrayList<RabbitMqMap> rmList = new ArrayList<>();
        XSSFWorkbook execl = new XSSFWorkbook();

//
        ListInstancesResponse listInstancesResponse = rabbitMqComponent.getRmResponse();
        List<ListInstancesResponseBody.ListInstancesResponseBodyDataInstances> instances = listInstancesResponse.getBody().getData().getInstances();



        for (ListInstancesResponseBody.ListInstancesResponseBodyDataInstances instance : instances) {


            RabbitMqMap rabbitMqMap = new RabbitMqMap();
            rabbitMqMap.setInstanceId(instance.getInstanceId());
            rabbitMqMap.setInstanceName(instance.getInstanceName());
            rabbitMqMap.setInstanceType(instance.getInstanceType());
            rabbitMqMap.setOrderType(instance.getOrderType());
            rabbitMqMap.setPrivateEndpoint(instance.getPrivateEndpoint());
            if (instance.getTags() != null){
                List<ListInstancesResponseBody.ListInstancesResponseBodyDataInstancesTags> tags = instance.getTags();
                for (ListInstancesResponseBody.ListInstancesResponseBodyDataInstancesTags tag : tags) {
                    if (tag.getKey().equals("lll:deployment:environment")){
                        rabbitMqMap.setEnvironment(tag.getValue());
                    }
                }
            }


            // request vHost
            ListVirtualHostsRequest listVirtualHostsRequest = new ListVirtualHostsRequest();
            listVirtualHostsRequest.setMaxResults(100).setInstanceId(instance.getInstanceId());
            ListVirtualHostsResponse listVirtualHostsResponse = rabbitMqComponent.getRmClient().listVirtualHostsWithOptions(listVirtualHostsRequest,teaopenapiClient.getRunTime());
            List<ListVirtualHostsResponseBody.ListVirtualHostsResponseBodyDataVirtualHosts> virtualHosts = listVirtualHostsResponse.getBody().getData().getVirtualHosts();
            ArrayList<RabbitMqVhostMap> vhostMaps = new ArrayList<>();
            for (ListVirtualHostsResponseBody.ListVirtualHostsResponseBodyDataVirtualHosts virtualHost : virtualHosts) {
                RabbitMqVhostMap rabbitMqVhostMap = new RabbitMqVhostMap();
                rabbitMqVhostMap.setName(virtualHost.getName());
                vhostMaps.add(rabbitMqVhostMap);
            }
            rabbitMqMap.setRabbitMqVhostMap(vhostMaps);

            // request exChange
            ArrayList<RabbitMqExchangeMap> rabbitMqExchangeMaps = new ArrayList<>();
            ArrayList<RabbitMqQueueMap> rabbitMqQueueMaps = new ArrayList<>();


            for (RabbitMqVhostMap vhostMap : vhostMaps) {

                // request exChange
                ListExchangesRequest listExchangesRequest = new ListExchangesRequest();
                listExchangesRequest.setInstanceId(instance.getInstanceId());
                listExchangesRequest.setMaxResults(100);
                listExchangesRequest.setVirtualHost(vhostMap.getName());
                ListExchangesResponse listExchangesResponse = rabbitMqComponent.getRmClient().listExchangesWithOptions(listExchangesRequest, teaopenapiClient.getRunTime());
                List<ListExchangesResponseBody.ListExchangesResponseBodyDataExchanges> exchanges = listExchangesResponse.getBody().getData().getExchanges();
                for (ListExchangesResponseBody.ListExchangesResponseBodyDataExchanges exchange : exchanges) {
                    if ( ! exchange.getName().equals("amq.direct") && ! exchange.getName().equals("amq.topic") && ! exchange.getName().equals("amq.fanout")) {
                        RabbitMqExchangeMap rabbitMqExchangeMap = new RabbitMqExchangeMap();
                        rabbitMqExchangeMap.setExchangeName(exchange.getName());
                        rabbitMqExchangeMap.setVHostName(exchange.getVHostName());
                        rabbitMqExchangeMap.setExchangeType(exchange.getExchangeType());
                        rabbitMqExchangeMaps.add(rabbitMqExchangeMap);
                    }
                }

                // request queue
                ListQueuesRequest listQueuesRequest = new ListQueuesRequest();
                listQueuesRequest.setInstanceId(instance.getInstanceId());
                listQueuesRequest.setMaxResults(100);
                listQueuesRequest.setVirtualHost(vhostMap.getName());
                ListQueuesResponse listQueuesResponse = rabbitMqComponent.getRmClient().listQueuesWithOptions(listQueuesRequest, teaopenapiClient.getRunTime());
                List<ListQueuesResponseBody.ListQueuesResponseBodyDataQueues> queues = listQueuesResponse.getBody().getData().getQueues();
                for (ListQueuesResponseBody.ListQueuesResponseBodyDataQueues queue : queues) {
                    RabbitMqQueueMap rabbitMqQueueMap = new RabbitMqQueueMap();
                    rabbitMqQueueMap.setQueueName(queue.getName());
                    rabbitMqQueueMap.setInstanceId(queue.getOwnerId());
                    rabbitMqQueueMap.setVHostName(queue.getVHostName());
                    rabbitMqQueueMaps.add(rabbitMqQueueMap);
                }

            }
            rabbitMqMap.setRabbitMqExchangeMap(rabbitMqExchangeMaps);
            rabbitMqMap.setRabbitMqQueueMap(rabbitMqQueueMaps);

            XSSFSheet sheetName = execl.createSheet();
            XSSFRow row = sheetName.createRow(0);

            if (row.getCell(0) == null ){
                row.createCell(0).setCellValue("instanceId");
                row.createCell(1).setCellValue("instanceName");
                row.createCell(2).setCellValue("environment");
                row.createCell(3).setCellValue("vHostName");
                row.createCell(4).setCellValue("exchangeName");
                row.createCell(5).setCellValue("exchangeType");
                row.createCell(6).setCellValue("queueName");
            }




            int maxNum;
            int exchangeNum = rabbitMqExchangeMaps.size();
            int queueNum = rabbitMqQueueMaps.size();
            maxNum = (exchangeNum > queueNum ) ? exchangeNum : queueNum;

            for (int i = 1; i < maxNum+1; i++) {
                sheetName.createRow(i);
            }

            XSSFRow row1 = sheetName.getRow(1);
            row1.createCell(0).setCellValue(rabbitMqMap.getInstanceId());
            row1.createCell(1).setCellValue(rabbitMqMap.getInstanceName());
            row1.createCell(2).setCellValue(rabbitMqMap.getEnvironment());

            ArrayList<RabbitMqVhostMap> rabbitMqVhostMap = rabbitMqMap.getRabbitMqVhostMap();
            for (int i = 0; i < rabbitMqVhostMap.size(); i++) {
                XSSFRow row11 = sheetName.getRow(i+1);
                row11.createCell(3).setCellValue(rabbitMqVhostMap.get(i).getName());
            }

            ArrayList<RabbitMqExchangeMap> rabbitMqExchangeMap = rabbitMqMap.getRabbitMqExchangeMap();
            for (int i = 0; i < rabbitMqExchangeMap.size(); i++) {
                XSSFRow row11 = sheetName.getRow(i + 1);
                row11.createCell(4).setCellValue(rabbitMqExchangeMap.get(i).getVHostName() + ":" + rabbitMqExchangeMap.get(i).getExchangeName());
                row11.createCell(5).setCellValue(rabbitMqExchangeMap.get(i).getExchangeType());
            }

            ArrayList<RabbitMqQueueMap> rabbitMqQueueMap = rabbitMqMap.getRabbitMqQueueMap();
            for (int i = 0; i < rabbitMqQueueMap.size(); i++) {
                XSSFRow row11 = sheetName.getRow(i + 1);
                row11.createCell(6).setCellValue(rabbitMqQueueMap.get(i).getVHostName() + ":" + rabbitMqQueueMap.get(i).getQueueName());
            }

        }
        FileOutputStream fileOutputStream = new FileOutputStream("mq.xlsx");
        execl.write(fileOutputStream);
        fileOutputStream.close();
        execl.close();

    }

}
